package com.drinks.yiniang.adapter.user.order;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.adapter.listview.UploadImageGridAdapter;
import com.drinks.yiniang.common.listener.OnListItem2ClickListener;
import com.drinks.yiniang.entity.common.RadarEntity;
import com.drinks.yiniang.entity.common.UploadImageJson;
import com.drinks.yiniang.entity.user.order.OrderEvaluateJson;
import com.drinks.yiniang.ui.widget.layout.DimensionLayout;
import com.drinks.yiniang.ui.widget.layout.DimensionWrapLayout;
import com.drinks.yiniang.ui.widget.view.RadarMapView;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.listener.OnListItemClickListener;
import com.hansen.library.ui.widget.image.RatioRoundImageView;
import com.hansen.library.ui.widget.listview.NoScrollGridView;
import com.hansen.library.ui.widget.ratingbar.CustomRatingBar;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.LogUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 待评价商品列表adapter
 * @author han
 * @date 2019/8/30
 */
public class OrderEvaluateAdapter extends BaseQuickRCVAdapter<OrderEvaluateJson.OrderEvaluateGoods, BaseViewHolder> {

    private EditText mEditText;
    private CustomRatingBar mRatingBar;
    //雷达图
    private RadarMapView mRadarMapView;
    //多维度layout
    private DimensionWrapLayout mDimensionWrap;

    private UploadImageGridAdapter mUploadImageGridAdapter;

    private OnListItem2ClickListener onListItem2ClickListener;
    private CustomRatingBar.OnRatingChangeListener onRatingChangeListener;
    private DimensionWrapLayout.OnDimensionRatingBarChangeListener onDimensionRatingBarChangeListener;

    private TextWatcher mWatcher;

    private List<RadarEntity> mRadarList;
    private RadarEntity mRadarEntity;

    //当前用户是否专家评论
    private boolean isSpecialistComment;

    public OrderEvaluateAdapter(Context mContext, @Nullable List<OrderEvaluateJson.OrderEvaluateGoods> data) {
        super(R.layout.item_order_evaluate, data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final OrderEvaluateJson.OrderEvaluateGoods item) {
        //商品图
        GlideUtils.loadSquareImage(mContext, (RatioRoundImageView) helper.getView(R.id.iv_order_evaluate_cover), item.getCover_url());
        //商品名称
        helper.setText(R.id.tv_order_evaluate_name, item.getGoods_name());
        //推荐语
        helper.setText(R.id.tv_order_evaluate_slogan, item.getRecommend_reason());
        //评价语
        setEvaluateContent(helper, item);
        //评分
        setGoodsRatingBar(helper, item);
        //专家评分
        setRadarRatingBar(helper, item);
        //图片
        setGridImage(helper, item);
    }

    private void setEvaluateContent(final BaseViewHolder helper, OrderEvaluateJson.OrderEvaluateGoods item) {
        mWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                //每次修改文字后，保存在数据集合中
                getItem(helper.getAdapterPosition()).setCommentContent(editable.toString());
            }
        };

        mEditText = helper.getView(R.id.et_order_evaluate_content);

        if(mEditText.getTag() instanceof TextWatcher) {
            mEditText.removeTextChangedListener(mWatcher);
        }

        mEditText.setText(item.getCommentContent());
        mEditText.setTag(mWatcher);
        mEditText.addTextChangedListener(mWatcher);
    }

    //商品评分
    private void setGoodsRatingBar(final BaseViewHolder helper, OrderEvaluateJson.OrderEvaluateGoods item) {
        onRatingChangeListener = new CustomRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(CustomRatingBar ratingBar, float ratingCount) {
                getItem(helper.getAdapterPosition()).setGoodsScore(ratingCount);
            }
        };

        mRatingBar = helper.getView(R.id.rating_order_evaluate);

        if(mRatingBar.getTag() instanceof CustomRatingBar.OnRatingChangeListener) {
            mRatingBar.setOnRatingChangeListener(null);
        }

        mRatingBar.setStar(item.getGoodsScore());
        mRatingBar.setOnRatingChangeListener(onRatingChangeListener);
        mRatingBar.setTag(onRatingChangeListener);
    }

    //设置雷达评分
    private void setRadarRatingBar(final BaseViewHolder helper, final OrderEvaluateJson.OrderEvaluateGoods item) {
        mRadarMapView = helper.getView(R.id.v_order_evaluate_radar_map);
        mDimensionWrap = helper.getView(R.id.ll_order_evaluate_dimension);

        if(!isSpecialistComment || CommonUtils.getListSize(item.getDimensionScores()) < 5) {
            mDimensionWrap.setVisibility(View.GONE);
            mRadarMapView.setVisibility(View.GONE);
            return;
        }

        mRadarList = new ArrayList<>();

        for(OrderEvaluateJson.OrderEvaluateRadar radar : item.getDimensionScores()) {
            mRadarEntity = new RadarEntity(radar.getName(), radar.getScoreValue());
            mRadarList.add(mRadarEntity);
        }

        mRadarMapView.setVisibility(View.VISIBLE);
        mDimensionWrap.setVisibility(View.VISIBLE);
        mRadarMapView.setRadarEvaluateData(mRadarList, 5);

        if(mDimensionWrap.getTag() instanceof DimensionWrapLayout.OnDimensionRatingBarChangeListener) {
            mDimensionWrap.setOnDimensionRatingBarChangeListener(null);
        }

        onDimensionRatingBarChangeListener = new DimensionWrapLayout.OnDimensionRatingBarChangeListener() {
            @Override
            public void onDimensionRatingBarChange(CustomRatingBar view, float rating, int childPosition) {
                OrderEvaluateJson.OrderEvaluateGoods temData = getItem(helper.getAdapterPosition());

                if(temData == null || CommonUtils.isArrayIndexOutOfBounds(temData.getDimensionScores(), childPosition)) {
                    return;
                }

                temData.getDimensionScores().get(childPosition).setScoreValue(rating);
                setData(helper.getAdapterPosition(), temData);
            }
        };

        mDimensionWrap.addDimensionLayout(item.getDimensionScores());
        mDimensionWrap.setOnDimensionRatingBarChangeListener(onDimensionRatingBarChangeListener);
        mDimensionWrap.setTag(onDimensionRatingBarChangeListener);
    }

    //设置图片列表
    private void setGridImage(final BaseViewHolder helper, OrderEvaluateJson.OrderEvaluateGoods item) {
        mUploadImageGridAdapter = new UploadImageGridAdapter(mContext, item.getImageIds());

        NoScrollGridView gridView = helper.getView(R.id.gv_order_evaluate_photo);

        mUploadImageGridAdapter.setOnListItemClickListener(new OnListItemClickListener() {
            @Override
            public void onItemClick(View mView, int position) {
                if(onListItem2ClickListener != null) {
                    onListItem2ClickListener.onItemClick(mView, helper.getAdapterPosition(), position);
                }
            }
        });

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(onListItem2ClickListener != null) {
                    onListItem2ClickListener.onItemClick(view, helper.getAdapterPosition(), position);
                }
            }
        });

        gridView.setAdapter(mUploadImageGridAdapter);
    }

    //获取是否是专家
    public boolean isSpecialistComment() {
        return isSpecialistComment;
    }

    public void setSpecialistComment(boolean specialistComment) {
        isSpecialistComment = specialistComment;
    }

    public void setOnListItem2ClickListener(OnListItem2ClickListener onListItem2ClickListener) {
        this.onListItem2ClickListener = onListItem2ClickListener;
    }
}
